package com.paw.quartz.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.paw.quartz.dto.JobRequestDto;
import com.paw.quartz.service.QuartzService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lvzihai
 * @date 2021/8/12
 **/
@Api(tags = "Quartz控制器")
@RestController
public class QuartzController {


  @Autowired
  private QuartzService quartzService;

  @ApiOperation("添加任务")
  @PostMapping("/addJob")
  public Object addJob (@RequestBody JobRequestDto requestDto){
    Object bean = SpringUtil.getBean(requestDto.getBeanName());
    if(!(bean instanceof QuartzJobBean)){
      return "bean type is not QuartzJobBean";
    }
    QuartzJobBean jobBean = (QuartzJobBean) bean;
    quartzService.addJob(jobBean.getClass(),requestDto.getJobName(),requestDto.getJobGroupName(),requestDto.getCron(),requestDto.getParams());

    return "success";
  }

}
